/**
 * parses data into an AST
 */
#include <stdlib.h>
#include "language_scanner.h"
#include "ypl_parser.h"
#include "ast.h"
#include "debug.h"

AST_node* parse_language(scanner_state *state) {
	AST_node *r;
	token_value *token=NULL;
	void *pParser;
	int scanner_state;
	size_t lexem_len=0;//number of lexems in the parse tree

	r = ast_malloc_node(top,none,root);
	if(!r) {
		return NULL;
	}
	pParser = ParseAlloc(malloc);
	if(!pParser) {
		free(r);
		return NULL;
	}
	while((scanner_state = scan(state,&token)) > 0) {
		lexem_len++;
		Parse(pParser,scanner_state,token,r);
	}
	if(0 == scanner_state) {
		Parse(pParser,scanner_state,token,r);
	}
	else {
		//error
	}
	ParseFree(pParser,free);
	printf("number of lexems: %d\n",lexem_len);
	return r;
}
